package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala.function.WindowFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector

object Demo7Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS
      .flatMap(_.split(","))
      .map((_, 1))

    /**
     * 每隔5秒统计最近5秒单词的数量
     */

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * TumblingProcessingTimeWindows： 滚动的处理时间窗口
     */
    val windowDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS
      .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))

    //在窗口内聚合计算
    val countDS: DataStream[(String, Int)] = windowDS.sum(1)

    //countDS.print()

    /**
     *
     * 在window止呕使用apply: 可以对同一个窗口内的数据做任意计算
     */

    val applyDS: DataStream[(String, Int)] = windowDS.apply(new WindowFunction[(String, Int), (String, Int), String, TimeWindow] {
      /**
       * apply: 每一个窗口运行一次
       *
       * @param key    ： keyBy的key的数据，单词
       * @param window ： 代表窗口的对象,可以获取窗口的开始和接收时间
       * @param input  ： 一个窗口内所有的数据
       * @param out    ： 用于将数据发送到下游
       */
      override def apply(key: String,
                         window: TimeWindow,
                         input: Iterable[(String, Int)],
                         out: Collector[(String, Int)]): Unit = {

        val start: Long = window.getStart
        val end: Long = window.getEnd
        println(s"窗口开始时间：$start")
        println(s"窗口结束时间：$end")

        println(s"窗口被计算：$key")
        //统计单词的数量
        val count: Int = input.size
        //将结果发送到下游
        out.collect((key, count))
      }
    })

    applyDS.print()

    env.execute()
  }

}
